var form_pics = (function(){
	var fp = function(){
		this.length = 1;
		this.maxlength = 3;
		this.videoLength = 0;
		this.length_array=new Array();
		for(var lengths = 1; lengths <= this.maxlength; lengths++){
			this.length_array[lengths]=lengths;
		}
	}

	fp.prototype = {
		addImg: function(thi){
			if(thi.files && thi.files[0]){
				var img = thi.nextSibling;
				var num=this.length;
				if(thi.files[0].type.indexOf("video")==-1 ){
					var URL = window.URL||webkitURL;
					var url = URL.createObjectURL(thi.files[0]);
					img.src = url;
					thi.parentNode.setAttribute("type","image"+num);
				}else{
					if(this.videoLength>=1){
						alert("只能上传一个视频", 1000);
						return this;
					}
					img.src = 'imgs/3.png';
					this.videoLength +=1;
					thi.parentNode.setAttribute("type","video");
				}
				this.length ++;
				this.createImgFile(thi);
				thi.setAttribute("style","display:none;");
				/**/
				$.ajaxFileUpload({
					url: myurl,
					dataType:"json",
					fileElementId: thi.id,//fileElementId: 'file_pic'+num,
					success: function(data, status) {
						console.log(data.content);
						if(data.error==0){
							console.log(data.url);
							$("#text_pic").val(data.url);
							var inputss = '<input type="hidden" id="image'+num+'" name="img[]" value="' + data.url + '">';
                    		$('#newthread').append(inputss);
						}else{
							alert(data.content);
						}
					},
					error: function(data, status, e) {
						console.log(data);
						alert(data.content);
					}
				});
				
				return this;
			}
		},
		removeImg: function(thi){
			var type = $(thi).closest("dd").attr("type");
			$('#'+type).remove();
			if("video" == type ){
				this.videoLength -=1;
			}
			this.createImgFiledelzsd(thi);
			return this;
		},
		createImgFiledelzsd: function(thi){
			if(this.length>this.maxlength){
				var TPL = '<dd><input type="file" id="file_pic'+this.getTimeStamp()+'" accept="image/jpg, image/jpeg, image/png"  onchange="form_pics.addImg(this);" name="pics[]" /><img src="" />\
							<span onclick="form_pics.removeImg(this);">&nbsp;</span></dd><dt>\
								<label>最多可上传'+this.maxlength+'张图片</label>\
							</dt>';
				$(thi).closest("dl").append($(TPL) );
			}
			$(thi).closest("dd").remove();
			this.length --;
			//this.createImgFile(thi);
		},
		createImgFile: function(thi){
			if(this.length>this.maxlength){
				//this.length = this.maxlength;
				var TPL = '<dd><img src="" />\
							<span onclick="form_pics.removeImg(this);">&nbsp;</span></dd><dt>\
								<label>最多可上传'+this.maxlength+'张图片</label>\
							</dt>';
				return this;
			}
			var TPL = '<dd><input type="file" id="file_pic'+this.getTimeStamp()+'" accept="image/jpg, image/jpeg, image/png"  onchange="form_pics.addImg(this);" name="pics[]" /><img src="" />\
							<span onclick="form_pics.removeImg(this);">&nbsp;</span></dd><dt>\
								<label>最多可上传'+this.maxlength+'张图片</label>\
							</dt>';
			$(thi).closest("dl").append($(TPL) );
			return this;
		},
		getTimeStamp: function(){
		    var timestamp = Date.parse(new Date());
		    return timestamp;
		}
	}
	return new fp();
})();
